#include <stdio.h>
int table1[] = {8, 15, 19, 26, 33, 41, 47, 52, 64, 90};//查找表1
int size1 = sizeof(table1) / sizeof(table1[0]);
int cnt;//记录查找次数
int binarySearch(int key) {
	int low = 0, high = size1 - 1, mid;
	while (low <= high) {
		mid = low + (high - low) / 2;
		if (table1[mid] == key) {
			return mid; // 返回找到的索引
		} else if (table1[mid] < key) {
			low = mid + 1;
		} else {
			high = mid - 1;
		}
		cnt++;//查找次数+1
	}
	return -1; 
}
int main() {
	int key = 41;
	int index = binarySearch(key);
	printf("查找key=%d的折半查找算法：\n", key);
	if (index != -1) {
		printf("找到，索引：%d,比较次数：%d\n", index,cnt);
	} else {
		printf("没找到\n");
	}
	return 0;
}

